﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NBusiness.Data.Providers.SqlServer;
using NBusiness.Query.Testing.Mock;
using NBusiness.Data.Testing.Mocks;

namespace NBusiness.Query.Testing
{
    [TestClass]
    class SqlLoaderTests
    {
        [TestMethod]
        public void TestContainsValue()
        {
            MockDataReader dr = new MockDataReader();
            ILoader loader = new SqlLoader(dr);
                        
            bool actual = loader.ContainsValue("c1");
            Assert.AreEqual(true, actual);

            Assert.AreEqual("GetOrdinal", MethodCall.Calls[0].Method);
            Assert.AreEqual("c1", MethodCall.Calls[0].Values["column"]);
        }

        [TestMethod]
        public void TestGetValue()
        {
            MockDataReader dr = new MockDataReader();
            ILoader loader = new SqlLoader(dr);

            int actual = loader.GetValue<int>("c1");
            Assert.AreEqual(100, actual);

            Assert.AreEqual("GetOrdinal", MethodCall.Calls[0].Method);
            Assert.AreEqual("c1", MethodCall.Calls[0].Values["column"]);

            Assert.AreEqual("IsDBNull", MethodCall.Calls[1].Method);
            Assert.AreEqual(0, MethodCall.Calls[1].Values["i"]);

            Assert.AreEqual("GetValue", MethodCall.Calls[2].Method);
            Assert.AreEqual(0, MethodCall.Calls[2].Values["i"]);
        }

        [TestMethod]
        public void TestNext()
        {
            MockDataReader dr = new MockDataReader();
            ILoader loader = new SqlLoader(dr);

            loader.Next();

            Assert.AreEqual("NextResult", MethodCall.Calls[0].Method);
        }

        [TestMethod]
        public void TestRead()
        {
            MockDataReader dr = new MockDataReader();
            ILoader loader = new SqlLoader(dr);

            loader.Read();

            Assert.AreEqual("Read", MethodCall.Calls[0].Method);
        }

        [TestMethod]
        public void TestDispose()
        {
            MockDataReader dr = new MockDataReader();
            ILoader loader = new SqlLoader(dr);

            loader.Dispose();

            Assert.AreEqual("Dispose", MethodCall.Calls[0].Method);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void NullDataReaderFail()
        {
            new SqlLoader(null);
        }
    }
}
